package myATMCaseV1;

import java.math.BigDecimal;
import java.util.Scanner;

public class UserView {
    Scanner scanner = new Scanner(System.in);
    AccountControl accountControl = new AccountControl();

    //登陆界面
    public void userLogin(){
        System.out.println("-￥-三国银行欢迎您-￥-");
        boolean isFlag = true;
        while (isFlag){
            System.out.println();
            System.out.println("|-登*陆*账*户-|");
            System.out.println("请选择：1-登陆 | 2-退出");
            int inputNum = scanner.nextInt();
            switch (inputNum){
                case 1:
                    System.out.print("请输入银行卡账号：");
                    Integer uid = scanner.nextInt();
                    System.out.print("请输入密码：");
                    String passwordFrom = scanner.next();
                    boolean isLogin = accountControl.login(uid, passwordFrom);
                    if(isLogin){
                        System.out.println("登陆成功！");
                        mainView();//进入主界面
                    }else{
                        System.out.println();
                        System.out.println("登陆失败：账号或密码错误！请重新输入...");
                    }
                    break;
                case 2:
                    System.out.println("*-*感谢您的使用，欢迎下次光临*-*");
                    isFlag = false;
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入...");
            }
        }
    }

    //主界面
    public void mainView(){
        boolean isFlag = true;
        while (isFlag){
            System.out.println();
            System.out.println("-￥- 主界面 -￥-");
            System.out.println("请选择业务：1-存款 | 2-转账 | 3-查看余额 | 4-取款 | 5-查询信息 | 6-修改信息 | 7-退出");
            int inputNum = scanner.nextInt();
            switch (inputNum){
                case 1: deposit(); break;
                case 2: transfer(); break;
                case 3: checkBalance(); break;
                case 4: withdrawal(); break;
                case 5: searchInfo(); break;
                case 6: modifyInfo(); break;
                case 7: quit();
                isFlag = false;
                break;
                default: System.out.println("您的输入有误，请重新输入...");
            }
        }
    }

    //存款
    public void deposit(){
        System.out.println();
        System.out.println("-￥- 存款业务 -￥-");
        boolean isFlag = true;
        while (isFlag){
            System.out.print("请输入存款金额：");
            BigDecimal money = scanner.nextBigDecimal();
            if(money.compareTo(BigDecimal.valueOf(0)) == 1){//BigDecimal可以通过compareTo方法来进行比较。返回的值是int类型，-1表示小于，0是等于，1是大于。
                boolean isNewFlag = true;
                while (isNewFlag){
                    System.out.println("请确认：1-确认存款 | 2-取消");
                    int inputNum = scanner.nextInt();
                    switch (inputNum){
                        case 1:
                            boolean isWithdrawal = accountControl.deposit(money);
                            if(isWithdrawal){
                                System.out.println("您的账户成功存入" + money + "元");
                                System.out.println("目前账户余额为：" + accountControl.checkBalance() + "元");
                                System.out.println("存款已完成，自动退回主界面！");
                                isNewFlag = false;
                                isFlag = false;
                            }else{
                                System.out.println("存款失败！");
                                System.out.println("目前账户余额为：" + accountControl.checkBalance() + "元");
                                System.out.println("请重新进行存款！");
                                isNewFlag = false;
                            }
                            break;
                        case 2:
                            System.out.println("本次存款已取消，已自动退回主界面！");
                            isNewFlag = false;
                            isFlag = false;
                            break;
                        default:
                            System.out.println("您的输入有误，请重新输入...");
                    }
                }
            }else{
                System.out.println("您输入的金额有误，请重新输入...");
            }
        }
    }

    //转账
    public void transfer(){
        System.out.println();
        System.out.println("-￥- 转账业务 -￥-");
        boolean isFlag = true;
        while (isFlag){
            System.out.print("请输入对方的银行卡号(uid)：");
            Integer uid = scanner.nextInt();
            System.out.print("请输入转账金额：");
            BigDecimal money = scanner.nextBigDecimal();
            if(money.compareTo(BigDecimal.valueOf(0)) == 1){
                Account returnAccount = accountControl.queryAccount(uid);
                if(returnAccount == null){
                    System.out.print("输入有误，请重新输入银行卡号：");
                }else{
                    System.out.print("请再次确认：是否给银行卡号为" + returnAccount.getUid() + "的用户进行转账！");
                    System.out.println("转账金额为：" + money + "元");
                    boolean isNewFlag = true;
                    while(isNewFlag){
                        System.out.println("请选择：1-确认转账 | 2-取消");
                        int inputNum = scanner.nextInt();
                        switch (inputNum){
                            case 1:
                                boolean isTransfer = accountControl.transfer(returnAccount, money);
                                if(isTransfer){
                                    System.out.println("转账成功！！！");
                                    System.out.println("您目前的账户余额为：" + accountControl.checkBalance() + "元");
                                    System.out.println("转账已完成，自动退回主界面！");
                                    isNewFlag = false;
                                    isFlag = false;
                                }else{
                                    System.out.println("转账失败！");
                                    System.out.println("目前账户余额为：" + accountControl.checkBalance() + "元");
                                    System.out.println("请重新进行转账！");
                                    isNewFlag = false;
                                }
                                break;
                            case 2:
                                System.out.println("本次转账已取消，已自动退回主界面！");
                                isNewFlag = false;
                                isFlag = false;
                                break;
                            default:
                                System.out.println("您的输入有误，请重新输入...");
                        }
                    }
                }
            }else{
                System.out.println("您输入的金额有误，请重新输入...");
            }
        }
    }

    //查询余额
    public void checkBalance(){
        System.out.println();
        System.out.println("-￥- 查询余额 -￥-");
        BigDecimal balance = accountControl.checkBalance();
        System.out.println("您的账户余额为：" + balance + "元");
        System.out.println("查询余额已完成，自动退回主界面！");
    }

    //取款
    public void withdrawal(){
        System.out.println();
        System.out.println("-￥- 取款业务 -￥-");
        boolean isFlag = true;
        while (isFlag){
            System.out.print("请输入取款金额：");
            BigDecimal money = scanner.nextBigDecimal();
            if(money.compareTo(BigDecimal.valueOf(0)) == 1){//BigDecimal可以通过compareTo方法来进行比较。返回的值是int类型，-1表示小于，0是等于，1是大于。
                boolean isNewFlag = true;
                while (isNewFlag){
                    System.out.println("请确认：1-确认取款 | 2-取消");
                    int inputNum = scanner.nextInt();
                    switch (inputNum){
                        case 1:
                            boolean isWithdrawal = accountControl.withdrawal(money);
                            if(isWithdrawal){
                                System.out.println("您的账户成功取出" + money + "元");
                                System.out.println("目前账户余额为：" + accountControl.checkBalance() + "元");
                                System.out.println("取款已完成，自动退回主界面！");
                                isNewFlag = false;
                                isFlag = false;
                            }else{
                                System.out.println("取款失败！");
                                System.out.println("目前账户余额为：" + accountControl.checkBalance() + "元");
                                System.out.println("请重新进行取款！");
                                isNewFlag = false;
                            }
                            break;
                        case 2:
                            System.out.println("本次取款已取消，已自动退回主界面！");
                            isNewFlag = false;
                            isFlag = false;
                            break;
                        default:
                            System.out.println("您的输入有误，请重新输入...");
                    }
                }
            }else{
                System.out.println("您输入的金额有误，请重新输入...");
            }
        }
    }

    //查询信息
    public void searchInfo(){
        System.out.println();
        System.out.println("--- 账户信息 ---");
        Account account = accountControl.searchInfo();
        System.out.println("|- 账号：" + account.getUid());
        System.out.println("|- 用户名：" + account.getUsername());
        System.out.println("|- 密码：" + account.getPassword());
        System.out.println("|- 余额：" + account.getBalance());
        System.out.println("查询信息已完成，自动退回主界面！");
    }

    //修改信息
    public void modifyInfo(){
        System.out.println();
        System.out.println("--- 修改信息(必须修改，否则不能退回主界面) ---");
        boolean isFlag = true;
        while (isFlag){
            System.out.println("请选择：1-修改用户名 | 2-修改密码 | 3-同时修改用户名和密码");
            boolean isNewFlag = true;
            int inputNum = scanner.nextInt();
            while (isNewFlag) {
                switch (inputNum) {
                    case 1:
                        boolean isNewName = modifyInfoUsername();
                        if (isNewName) {
                            isNewFlag = false;
                            isFlag = false;
                        }
                        break;
                    case 2:
                        boolean isNewPassword = modifyInfoPassword();
                        if (isNewPassword) {
                            isNewFlag = false;
                            isFlag = false;
                        }
                        break;
                    case 3:
                        boolean isNewNameAll = modifyInfoUsername();
                        if (isNewNameAll) {
                            boolean isNewPasswordAll = modifyInfoPassword();
                            if (isNewPasswordAll) {
                                isNewFlag = false;
                                isFlag = false;
                            }
                        }
                        break;
                    default:
                        System.out.println("您的输入有误，请重新输入...");
                }
            }
        }
    }

    //修改用户名
    public boolean modifyInfoUsername(){
        System.out.println();
        System.out.print("请输入新用户名：");
        String newUsername = scanner.next();
        if(newUsername.matches("[a-zA-Z0-9_\u4e00-\u9fa5]+")){
            boolean isNewName = accountControl.modifyInfoUsername(newUsername);
            if(isNewName){
                System.out.println("用户名修改成功！");
                System.out.println("新用户名为：" + newUsername);
                System.out.println("修改已完成，自动退回主界面！");
                return true;
            }else{
                System.out.println("用户名修改失败！");
                System.out.println("请重新修改！");
                return false;
            }
        }else{
            System.out.println("输入的用户名不符合规范，请重新输入用户名...");
            System.out.println("用户名：至少一个汉字、数字、字母或下划线");
        }
        return false;
    }

    //修改密码
    public boolean modifyInfoPassword(){
        System.out.println();
        System.out.print("请输入新密码：");
        String newPassword = scanner.next();
        if(newPassword.matches("[a-zA-Z0-9_]+")){
            boolean isNewPassword = accountControl.modifyInfoPassword(newPassword);
            if(isNewPassword){
                System.out.println("密码修改成功！");
                System.out.println("新密码为：" + newPassword);
                System.out.println("修改已完成，自动退回主界面！");
            }else{
                System.out.println("密码修改失败！");
                System.out.println("请重新修改！");
            }
            return isNewPassword;
        }else{
            System.out.println("输入的密码不符合规范，请重新输入密码...");
            System.out.println("密码：至少一个数字、字母或下划线");
        }
        return false;
    }

    //退出
    public void quit(){
        System.out.println();
        System.out.println("正在退出...");
    }
}
